package com.zg.indiv.web;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.dao.indiv.entity.IndivSelector;
import com.zg.indiv.domain.req.selector.*;
import com.zg.indiv.domain.vo.IndivSelectorUseVo;
import com.zg.indiv.domain.vo.IndivSelectorVo;
import com.zg.indiv.service.IndivSelectorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lixin
 */
@Api(tags = {"数据选择器管理"})
@RestController
@RequiredArgsConstructor
public class IndivSelectorController {
    private final IndivSelectorService indivSelectorService;

    @ApiOperation("分页查询所有数据选择器")
    @PostMapping("/selector/page")
    public R<IPage<IndivSelectorVo>> page(@RequestBody @Valid IndivSelectorPageReq param) {
        IPage<IndivSelector> page = indivSelectorService.page(param);
        IPage<IndivSelectorVo> result = new Page<>();
        BeanUtil.copyProperties(page, result);
        return R.success(result);
    }

    @ApiOperation("新增数据选择器")
    @ApiResponse(code = 809004001, message = "选择器名称已被使用")
    @PostMapping("/selector/add")
    public R<Void> add(@RequestBody @Valid IndivSelectorAddReq param) {
        indivSelectorService.add(param);
        return R.success();
    }

    @ApiOperation("修改数据选择器")
    @ApiResponses({
            @ApiResponse(code = 809004001, message = "选择器名称已被使用"),
            @ApiResponse(code = 809004002, message = "请选择正确的数据选择器")
    })
    @PostMapping("/selector/edit")
    public R<Void> edit(@RequestBody @Valid IndivSelectorEditReq param) {
        indivSelectorService.edit(param);
        return R.success();
    }

    @ApiOperation("删除数据选择器")
    @ApiResponse(code = 809004003, message = "数据选择器已被使用无法删除")
    @PostMapping("/selector/remove")
    public R<Void> remove(@RequestBody List<Long> idList) {
        indivSelectorService.remove(idList);
        return R.success();
    }

    @ApiOperation("查询数据选择器")
    @ApiResponse(code = 809004002, message = "请选择正确的数据选择器")
    @PostMapping("/selector/get")
    public R<IndivSelectorVo> get(@RequestBody @Valid IndivSelectorGetReq param) {
        IndivSelectorVo result = indivSelectorService.get(param);
        return R.success(result);
    }

    @ApiOperation("添加选择器表单")
    @ApiResponses({
            @ApiResponse(code = 809004002, message = "请选择正确的数据选择器"),
            @ApiResponse(code = 809004004, message = "请选择正确的数据选择器表单参数"),
            @ApiResponse(code = 809004005, message = "请选择正确的上级选择器表单关联"),
            @ApiResponse(code = 809004006, message = "表单名称已被使用"),
            @ApiResponse(code = 809004007, message = "参数名称已被使用"),
            @ApiResponse(code = 809004008, message = "目标参数字段已被使用")
    })
    @PostMapping("/selector/addForm")
    public R<Void> addForm(@RequestBody @Valid IndivSelectorAddFormReq param) {
        indivSelectorService.addForm(param);
        return R.success();
    }

    @ApiOperation("修改选择器表单")
    @ApiResponses({
            @ApiResponse(code = 809004009, message = "请选择正确的选择器表单"),
            @ApiResponse(code = 809004004, message = "请选择正确的数据选择器表单参数"),
            @ApiResponse(code = 809004005, message = "请选择正确的上级选择器表单关联"),
            @ApiResponse(code = 809004006, message = "表单名称已被使用"),
            @ApiResponse(code = 809004007, message = "参数名称已被使用"),
            @ApiResponse(code = 809004008, message = "目标参数字段已被使用")
    })
    @PostMapping("/selector/editForm")
    public R<Void> editForm(@RequestBody @Valid IndivSelectorEditFormReq param) {
        indivSelectorService.editForm(param);
        return R.success();
    }

    @ApiOperation("删除选择器表单")
    @ApiResponse(code = 809004010, message = "数据选择器表单已被使用")
    @PostMapping("/selector/removeForm")
    public R<Void> removeForm(@RequestBody List<Long> idList) {
        indivSelectorService.removeForm(idList);
        return R.success();
    }

    @ApiOperation("使用选择器表单数据")
    @ApiResponses({
            @ApiResponse(code = 809004002, message = "请选择正确的数据选择器"),
            @ApiResponse(code = 809004009, message = "请选择正确的选择器表单"),
            @ApiResponse(code = 809004011, message = "请选择正确的使用方"),
            @ApiResponse(code = 809004012, message = "数据已被使用")
    })
    @PostMapping("/selector/addUseState")
    public R<Void> addUseState(@RequestBody @Valid IndivSelectorAddUseStateReq param) {
        indivSelectorService.addUseState(param);
        return R.success();
    }

    @ApiOperation("取消使用选择器表单数据")
    @PostMapping("/selector/removeUseState")
    public R<Void> removeUseState(@RequestBody @Valid IndivSelectorRemoveUseStateReq param) {
        indivSelectorService.removeUseState(param);
        return R.success();
    }

    @ApiOperation("分页查询选择器使用记录")
    @PostMapping("/selector/pageSelectorUse")
    public R<IPage<IndivSelectorUseVo>> pageSelectorUse(@RequestBody @Valid IndivSelectorUsePageReq param) {
        IPage<IndivSelectorUseVo> page = indivSelectorService.pageSelectorUse(param);
        return R.success(page);
    }
}
